﻿using CWM.Client.ViewModels;
using Microsoft.Practices.Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Controls.Models;
using Controls.ViewModels;

namespace CWM.Client
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private static ObservableCollection<MainTabItemViewModel> collection = new ObservableCollection<MainTabItemViewModel>();

        public MainWindow()
        {
            InitializeComponent();
        }

        private DelegateCommand<MainMenuItemModel> cmd = new DelegateCommand<MainMenuItemModel>((arg) =>
        {
            MainTabItemViewModel item = new MainTabItemViewModel();
            item.IsActive = true;
            item.Data = arg;
            item.Frame = new Frame()
            {
                Source = new Uri(item.Data.Page, UriKind.RelativeOrAbsolute),
                Margin = new Thickness(0, 0, 0, 0),
                NavigationUIVisibility = NavigationUIVisibility.Hidden
            };

            foreach (MainTabItemViewModel i in collection)
            {
                if (item.Data.Page.Equals(i.Data.Page)) //如果已经存在，直接聚焦
                {
                    i.IsActive = true;
                    return;
                }
            }
            collection.Add(item);

        });

        private void mainMenu_Loaded(object sender, RoutedEventArgs e)
        {
            //菜单数据

            MainMenuViewModel m1 = new MainMenuViewModel() { Title = "操作管理", Icon = "\xe6e0" };
            m1.Children = new ObservableCollection<MainMenuItemViewModel>();
            m1.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("清点", "/Views/OM/OrderInputPrePage.xaml"), cmd));
            m1.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("称重", "/Views/OM/OrderInputPage.xaml"), cmd));
            m1.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("装袋", "/Views/OM/OrderInputQuickPage.xaml"), cmd));
            m1.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("装袋查询", "/Views/OM/OrderCheckOutPage.xaml"), cmd));

            MainMenuViewModel m3 = new MainMenuViewModel() { Title = "客户服务", Icon = "\xe621" };
            m3.Children = new ObservableCollection<MainMenuItemViewModel>();
            m3.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("订单管理", "/Views/Service/OrderListPage.xaml"), cmd));
            

            MainMenuViewModel m4 = new MainMenuViewModel() { Title = "客户管理", Icon = "\xe60c" };
            m4.Children = new ObservableCollection<MainMenuItemViewModel>();
            m4.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("客户注册审核", "/Views/CM/CustomerRegistrationReviewPage.xaml"), cmd));
            m4.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("客户资料管理", "/Views/CM/CustomerDataManagementListPage.xaml"), cmd));
            m4.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("客户余额查询", "/Views/CM/CustomerBalanceInquiryPage.xaml"), cmd));
            m4.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("客户授信管理", "/Views/CM/CustomerCreditManagementPage.xaml"), cmd));

            MainMenuViewModel m5 = new MainMenuViewModel() { Title = "财务管理", Icon = "\xe6e4" };
            m5.Children = new ObservableCollection<MainMenuItemViewModel>();
            m5.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("查单", "/Views/FM/ReceiptPage.xaml"), cmd));
            m5.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("扣款", "/Views/FM/ChargebackPage.xaml"), cmd));

            MainMenuViewModel m6 = new MainMenuViewModel() { Title = "统计报表", Icon = "\xe601" };
            m6.Children = new ObservableCollection<MainMenuItemViewModel>();
            m6.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("报表内容", "/Views/RPT/ReportRunPage.xaml"), cmd));
            m6.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("报表设置", "/Views/RPT/ReportManagementPage.xaml"), cmd));
            m6.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("字典设置", "/Views/RPT/ReportDictionaryPage.xaml"), cmd));

            MainMenuViewModel m7 = new MainMenuViewModel() { Title = "系统管理", Icon = "\xe646" };
            m7.Children = new ObservableCollection<MainMenuItemViewModel>();
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("组织机构", "/Views/CF/OrganizationListPage.xaml"), cmd));
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("用户管理", "/Views/CF/UserManagementListPage.xaml"), cmd));
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("角色权限", "/Views/CF/RolePermissionsListPage.xaml"), cmd));
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("系统设置", "/Views/CF/SystemSettingPage.xaml"), cmd));
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("标签管理", "/Views/CF/LabelManagementPage.xaml"), cmd));
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("系统工具", "/Views/CF/ToolsPage.xaml"), cmd));
            m7.Children.Add(new MainMenuItemViewModel(new MainMenuItemModel("临时测试", "/Views/TestPage2018.xaml"), cmd));


            ObservableCollection<MainMenuViewModel> collection = new ObservableCollection<MainMenuViewModel>();
            collection.Add(m1);
            collection.Add(m3);
            collection.Add(m4);
            collection.Add(m5);
            collection.Add(m6);
            collection.Add(m7);

            this.mainMenu.ItemsSource = collection;
        }

        private void btnWinMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
            //this.Visibility = Visibility.Hidden;
        }

        private void btnWinMax_Click(object sender, RoutedEventArgs e)
        {
            MainWindowMiewModel vm = this.DataContext as MainWindowMiewModel;
            vm.IsMaximized = !vm.IsMaximized;
        }

        private void winTitle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //如果是双击事件，执行最大化窗口动作
            if (e.ClickCount > 1)
            {
                btnWinMax_Click(sender, e);
            }
            //如果是单击事件，执行窗体拖拽
            else
            {
                this.DragMove();
            }
        }

        private void btnWinClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = new MainWindowMiewModel();
            this.tabControl.ItemsSource = collection;

            //添加引导页
            MainTabItemViewModel item = new MainTabItemViewModel();
            item.IsActive = true;
            item.Data = new MainMenuItemModel("首页", "/Views/TestPage2018.xaml");
            item.Frame = new Frame()
            {
                Source = new Uri(item.Data.Page, UriKind.RelativeOrAbsolute),
                Margin = new Thickness(0, 0, 0, 0),
                NavigationUIVisibility = NavigationUIVisibility.Hidden
            };
            collection.Add(item);
        }

        private void CloseX_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock btn = sender as TextBlock;
            string header = btn.Tag.ToString();
            if (collection.Count < 1)
            {
                return;
            }
            foreach (var item in collection)
            {
                if (item.Data.Title.ToString() == header)
                {
                    collection.Remove(item);
                    return;
                }
            }
        }

    }
}
